package cn.kicsy.www.anonymous_bbs.controller;

import cn.kicsy.www.anonymous_bbs.entity.Notification;
import cn.kicsy.www.anonymous_bbs.service.NotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.media.ArraySchema;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

@Tag(name = "通知管理", description = "通知相关的API接口")
@RestController
@RequestMapping("/api/notifications")
public class NotificationController {
    
    @Autowired
    private NotificationService notificationService;
    
    /**
     * 获取未读通知列表
     * @param request HTTP请求，用于获取用户IP地址
     * @return 未读通知列表，按创建时间降序排列
     */
    @Operation(summary = "获取未读通知", description = "获取当前用户的所有未读通知")
    @ApiResponse(responseCode = "200", description = "获取成功", content = @Content(array = @ArraySchema(schema = @Schema(implementation = Notification.class))))
    @GetMapping("/unread")
    public List<Notification> getUnreadNotifications(HttpServletRequest request) {
        String ipAddress = getClientIpAddress(request);
        return notificationService.getUnreadNotifications(ipAddress);
    }
    
    /**
     * 获取所有通知列表
     * @param request HTTP请求，用于获取用户IP地址
     * @return 所有通知列表，按创建时间降序排列
     */
    @Operation(summary = "获取所有通知", description = "获取当前用户的所有通知")
    @ApiResponse(responseCode = "200", description = "获取成功", content = @Content(array = @ArraySchema(schema = @Schema(implementation = Notification.class))))
    @GetMapping
    public List<Notification> getAllNotifications(HttpServletRequest request) {
        String ipAddress = getClientIpAddress(request);
        return notificationService.getAllNotifications(ipAddress);
    }
    
    /**
     * 标记单个通知为已读
     * @param notificationId 通知ID
     * @return 操作结果
     */
    @Operation(summary = "标记单条通知已读", description = "将指定的通知标记为已读状态")
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "标记成功", content = @Content(schema = @Schema(implementation = SuccessResponse.class)))
    })
    @PutMapping("/{notificationId}/read")
    public ResponseEntity<?> markNotificationAsRead(@PathVariable @Parameter(description = "通知ID") Long notificationId) {
        notificationService.markNotificationAsRead(notificationId);
        return ResponseEntity.ok(new SuccessResponse("通知已标记为已读"));
    }
    
    /**
     * 标记所有通知为已读
     * @param request HTTP请求，用于获取用户IP地址
     * @return 操作结果
     */
    @Operation(summary = "标记所有通知已读", description = "将当前用户的所有通知标记为已读状态")
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "标记成功", content = @Content(schema = @Schema(implementation = SuccessResponse.class)))
    })
    @PutMapping("/read-all")
    public ResponseEntity<?> markAllNotificationsAsRead(HttpServletRequest request) {
        String ipAddress = getClientIpAddress(request);
        notificationService.markAllNotificationsAsRead(ipAddress);
        return ResponseEntity.ok(new SuccessResponse("所有通知已标记为已读"));
    }
    
    /**
     * 获取客户端IP地址
     * @param request HTTP请求
     * @return 客户端IP地址
     */
    private String getClientIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        
        // 如果是多个IP地址，取第一个（通常是真实IP）
        if (ip != null && ip.contains(",")) {
            ip = ip.split(",")[0].trim();
        }
        
        return ip;
    }
    
    // 响应模型类
    @Schema(description = "成功响应")
    public static class SuccessResponse {
        private String message;
        
        public SuccessResponse(String message) {
            this.message = message;
        }
        
        public String getMessage() {
            return message;
        }
        
        public void setMessage(String message) {
            this.message = message;
        }
    }
}